package com.fun.easyplugin.dao;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.fun.easyplugin.dto.UserPO;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:huangzf1778@fingard.com">huang zifan</a>
 * @version 1.0
 * <p>
 * Copyright (c) 2009 fingard System Engineering Co., Ltd.
 * All rights reserved.
 * <p>
 * This software is the confidential and proprietary information of
 * fingard System Engineering Co., Ltd. ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered
 * into with fingard.
 * @projectName: easy-plugin
 * @date 2024/6/7
 */
@Component
public class UserDao extends AbsBaseDao {


    @SneakyThrows
    public List<UserPO> listUsers() {
        List<Entity> userList = Db.use()
                .findAll(Entity.create()
                        .setTableName("user"));
        return userList.stream()
                .map(this::buildUser)
                .collect(Collectors.toList());
    }

    private UserPO buildUser(Entity userEntity) {
        UserPO user = new UserPO();
        int id = userEntity.getInt("id");
        user.setId(id);
        String name = userEntity.getStr("name");
        user.setName(name);
        int age = userEntity.getInt("age");
        user.setAge(age);
        return user;
    }
}
